/**
 * 
 */
package cn.nawang.ebeim.client;

import java.util.Date;
import java.util.Iterator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.nawang.ebeim.client.bean.HeartLog;
import cn.nawang.ebeim.client.bean.UploadProcessBarBean;
import cn.nawang.ebeim.client.exception.ApiRequestException;
import cn.nawang.ebeim.client.util.ApiUtil;
import cn.nawang.ebeim.client.util.DbUpDownloadUtil;
import cn.nawang.ebeim.client.util.MACAddress;
import cn.nawang.ebeim.client.util.PropertiesConstants;
import cn.nawang.ebeim.client.util.PropertiesUtil;
import cn.nawang.ebeim.client.util.UploadProcessBarList;

/**
 * @ClassName: HeartLogThread
 * @Description: 心跳线程
 * @author zhangjf
 * @date 2015年10月28日13:56:20
 * @version v1.0
 */
public class HeartLogThread implements Runnable {

  private static final Logger LOG = LoggerFactory.getLogger(HeartLogThread.class);

  @Override
  public void run() {

    while (!AppMain.stopFlag.get()) {

      try {
        // 每3分钟向服务端插入一条心跳记录
        Thread.sleep(1000 * 60 * 3);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

      String uid = PropertiesUtil.read(PropertiesConstants.USER_ID);
      if (uid == null) {
        LOG.info("read uid failed...........................");
        continue;
      }
      LOG.info("read uid ok...........................");
      HeartLog heartLog = new HeartLog();
      heartLog.setActTime(new Date().getTime());
      heartLog.setActUserId(uid);
      heartLog.setLevel("3");
      heartLog.setSrcId("0");
      heartLog.setMsg("0");
      heartLog.setTerminalId(MACAddress.getMACAddr());

      String rs = "";
      try {
        rs = ApiUtil.addHeartLog(heartLog);
      } catch (ApiRequestException e) {
        LOG.error("fail to addFileChangeLog:{}", e);
        e.printStackTrace();
      }
      LOG.info("add heartlog result:" + rs);
    }

  }

}
